package com.liuqi.loggers.core.config;

import com.liuqi.loggers.core.Configuration;
import com.liuqi.loggers.core.Constants;
import com.liuqi.loggers.core.Level;
import com.liuqi.loggers.core.LoggingContext;
import com.liuqi.loggers.core.appender.ConsoleAppender;
import com.liuqi.loggers.core.config.xml.SaxRecorderConfiguration;
import java.io.InputStream;

/**
 * 初始化 {@link com.liuqi.loggers.core.Configuration}
 *
 * @author liuqi
 * @date 2024/6/7 16:26
 **/
public class GenericConfigurator {

    private LoggingContext context;

    public void doConfigure() {
        if (context == null) {
            throw new NullPointerException("context cannot be null");
        }
        ConsoleAppender appender = new ConsoleAppender();
        appender.setEncoder(Constants.defaultEncoder());
        Configuration configuration = new Configuration();
        configuration.setLevel(Level.TRACE);
        configuration.addAppender(appender);
        context.setConfiguration(configuration);
        context.start();
    }

    public void doConfigure(InputStream stream) throws Exception {
        if (context == null) {
            throw new NullPointerException("context cannot be null");
        }
        Configuration configuration = new SaxRecorderConfiguration(stream).record();
        context.setConfiguration(configuration);
        context.start();
    }

    public LoggingContext getContext() {
        return context;
    }

    public void setContext(LoggingContext context) {
        this.context = context;
    }
}
